using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Xml;

namespace SAE.Common
{
    /// <summary>
    /// XmlFileConfigManager
    /// </summary>
    public class XmlFileConfigManager
    {
        private XmlDocument ConfigDocument;

        private XmlNode DocumentRoot;

        private string ConfigFileName;

        private string RootTagName = "settings";

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlFileConfigManager"/> class.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        public XmlFileConfigManager(string fileName)
        {
            this.ConfigFileName =AppDomain.CurrentDomain.BaseDirectory+fileName;

            this.ConfigDocument = new XmlDocument();

            if (File.Exists(this.ConfigFileName))
            {
                this.ConfigDocument.Load(this.ConfigFileName);
            }
            else
            {
                XmlNode rootNode = this.ConfigDocument.CreateNode(XmlNodeType.Element, this.RootTagName, string.Empty);

                this.ConfigDocument.AppendChild(rootNode);

                this.Save();
            }

            this.DocumentRoot = this.ConfigDocument.SelectSingleNode(this.RootTagName);
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        private void Save()
        {
            this.ConfigDocument.Save(this.ConfigFileName);
        }

        /// <summary>
        /// Sets the item.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public bool SetItem(string key, string value)
        {
            XmlNode elementNode = this.DocumentRoot.SelectSingleNode(key);

            if (elementNode==null)
            {              
                elementNode = this.ConfigDocument.CreateNode(
    XmlNodeType.Element, key, string.Empty);

                elementNode.InnerText = value;

                this.DocumentRoot.AppendChild(elementNode);

                this.Save();

                return true;
            }
            else
            {
                elementNode.InnerText = value;

                //this.DocumentRoot.AppendChild(elementNode);

                this.Save();

                return true;
            }
        }

        /// <summary>
        /// Gets the item.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public string GetItem(string key)
        {         
            XmlNode elementNode = this.DocumentRoot.SelectSingleNode(key);

            if (elementNode != null)
            {
                return elementNode.InnerText;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Removes the item.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public bool RemoveItem(string key)
        {
            XmlNode elementNode = this.DocumentRoot.SelectSingleNode(key);

            if (elementNode != null)
            {
                this.DocumentRoot.RemoveChild(elementNode);

                this.Save();

                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
